package com.xb;

import jdk.internal.org.objectweb.asm.TypeReference;

import java.util.concurrent.CountDownLatch;

/**
 * @program: register-jerry
 * @description:  CountDownLanch这个类的使用，底层基于AQS
 * @author: xu.bin
 * @create: 2020-11-15
 */
public class CountDownLatchDemo {

    static public void main(String []args) throws InterruptedException {
        CountDownLatch countDownLatch=new CountDownLatch(2);
        new Thread(){
            @Override
            public void run() {
                try{
                    System.out.println("线程1准备执行....");
                    Thread.sleep(1000);
                    System.out.println("线程1准备执行countDown 操作");
                    countDownLatch.countDown();
                    System.out.println("线程1准备执行完成countDown 操作");

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
        new Thread(){
            @Override
            public void run() {
                try{
                    System.out.println("线程2准备执行....");
                    Thread.sleep(1000);
                    System.out.println("线程2准备执行countDown 操作");
                    countDownLatch.countDown();
                    System.out.println("线程2准备执行完成countDown 操作");

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
        System.out.println("主线程执行准备执行countDownLatch操作，await等待中...");
        countDownLatch.await();
        System.out.println("主线程执行准备执行countDownLatch操作，执行完成");
    }
}